<?php


namespace Plugin\PaypalSiteA;

use Beike\Services\StateMachineService;
use Illuminate\Support\Facades\Log;
use Matrix\Exception;
use Plugin\PaypalSiteA\Models\PollingSiteBSkus;
use Plugin\PaypalSiteA\Services\PaypalSiteTools;

class Bootstrap
{
    public function boot()
    {


        // 向B站创建订单

        add_hook_filter('repository.order.create.after', function ($data) {

            if ($data['order']->payment_method_code == 'paypal_site_a') {
                Log::debug("ffffffwww");
                $paypalSiteTools = new PaypalSiteTools();
                $domain          = $paypalSiteTools->getPollingSetting($data['order']->number);
                if (empty($domain)) {
                    die("请先配置支付站点");
                }
                $token       = null;
                $allSettings = plugin_setting($data['order']->payment_method_code . '.setting', []);
                foreach ($allSettings as $allSetting) {
                    if ($allSetting['domain'] == $domain) {
                        $token = $allSetting['token'];
                        break;
                    }
                }
                if (empty($token)) {
                    die("请先配置支付站点的Token");
                }
                $rs = $paypalSiteTools->httpPost($token, $domain, '/callback/paypal_s/create_order', json_encode($data, true));
                Log::debug($rs);
                if (isset($rs['code']) && $rs['code'] != 0) {//创建不成功
                    die($rs['msg']);
                }
                if (isset($rs['status']) && $rs['status'] == StateMachineService::PAID) {//已经支付了
                    StateMachineService::getInstance($data['order'])->changeStatus(StateMachineService::PAID, '', true);
                }

            }

            return $data;
        }, 20250511154612);

        add_hook_blade('admin.plugin.form', function ($callback, $output, $data) {
            $code = $data['plugin']->code;
            if ($code == 'paypal_site_a') {
                return view('PaypalSiteA::admin.config_form', $data)->render();
            }
            return $output;
        }, 20250511154612);

        add_hook_filter('service.payment.pay.data', function ($data) {
            if ($data['order']->payment_method_code == 'paypal_site_a') {
                $paypalSiteTools = new PaypalSiteTools();
                $domain          = $paypalSiteTools->getPollingSetting($data['order']->number);
                if (empty($domain)) {
                    die("请先配置支付站点");
                }
                $data['pay_url'] = $domain . '/orders/' . $data['order']->number . '/pay?type=create';
                //$data['order_no'] = $data['order']->number;
                // echo view('PaypalSiteA::shop.payment_iframe', $data);
                //exit;
            }

            return $data;
        }, 20250511154612);

        /**
         * 发货同步物流单号到paypal
         */
        add_hook_filter('service.state_machine.machines', function ($data) {
            $order = $data['order'];
            if ($order->payment_method_code == 'paypal_site_a') {
                $sendPaypalOrderShipping = function () use ($order) {
                    $orderShipment    = request()->all();
                    $expressCompanies = system_setting('base.express_company');
                    $tmpExpress       = [];
                    if (!empty($expressCompanies)) {
                        foreach ($expressCompanies as $expressCompanie) {
                            $tmpExpress[$expressCompanie['code']] = $expressCompanie['name'];
                        }
                    }

                    $order_number = $order->number;

                    //请求B站进行推送
                    $paypalSiteTools = new PaypalSiteTools();
                    $domain          = $paypalSiteTools->getPollingSetting($order_number);
                    if (empty($domain)) {
                        throw new Exception("请先配置支付站点");
                    }
                    $token       = null;
                    $allSettings = plugin_setting($order->payment_method_code . '.setting', []);
                    foreach ($allSettings as $allSetting) {
                        if ($allSetting['domain'] == $domain) {
                            $token = $allSetting['token'];
                            break;
                        }
                    }
                    if (empty($token)) {
                        throw new Exception("请先配置支付站点的Token");
                    }

                    $express_code   = $orderShipment['express_code'];
                    $express_number = $orderShipment['express_number'];
                    $notify         = request()->get('notify');
                    $comment        = request()->get('comment');
                    $status         = StateMachineService::SHIPPED;

                    $postData = [
                        'express_code'   => $express_code,
                        'express_number' => $express_number,
                        'notify'         => $notify,
                        'comment'        => $comment,
                        'order_no'       => $order_number,
                        'status'         => $status
                    ];
                    //Log::debug(json_encode($postData));
                    $rs = $paypalSiteTools->httpPost($token, $domain, '/callback/paypal_s/shipping', json_encode($postData, true));
                    if ($rs != 'success') {
                        //Log::debug($rs);
                        throw new Exception($rs);
                    }
                };
                array_unshift($data['machines'][StateMachineService::PAID][StateMachineService::SHIPPED], $sendPaypalOrderShipping);
            }
            return $data;
        }, 20250511154612);
    }


}
